package com.whfp.coky.manager

import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayoutMediator

/**
 *
 * tab管理类
 * created by coky on 2022/3/18
 */
class TabManage(var tab:TabLayout, var vp:ViewPager2) {

    fun initTab(activity: FragmentActivity, tabTexts:Array<String>, frags: Array<Fragment>){
        vp.adapter = object :FragmentStateAdapter(activity){
            override fun getItemCount() = frags.size
            override fun createFragment(position: Int) = frags[position]
        }
        TabLayoutMediator(tab,vp){
                tab, position ->
            tab.text = tabTexts[position]
        }.attach()
    }

    fun initTab(fragment: Fragment, tabTexts:Array<String>, frags: Array<Fragment>){
        vp.adapter = object :FragmentStateAdapter(fragment){
            override fun getItemCount() = frags.size
            override fun createFragment(position: Int) = frags[position]
        }
        TabLayoutMediator(tab,vp){
                tab, position ->
            tab.text = tabTexts[position]
        }.attach()
    }

}